<template>
  <div class="login">
    <div class="title">
      <h1>医养综合服务信息系统</h1>
    </div>
    <div class="banner"></div>
    <div class="footer">
      <h2 style="font-size: 30px">LOGO</h2>
      <br />
      <h3 style="font-size: 20px">
        欢迎使用<span id="title">医养综合服务信息系统</span>
      </h3>
      <br />
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
      >
        <el-form-item prop="username" style="margin-bottom: 20px">
          <el-input
            v-model="ruleForm.username"
            placeholder="请输入账号"
            style="font-size: 20px; height: 50px"
          />
        </el-form-item>
        <el-form-item prop="password" style="margin-bottom: 10px">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入密码"
            style="font-size: 20px; height: 50px"
          />
        </el-form-item>
        <el-form-item style="margin-bottom: 10px">
          <el-checkbox v-model="pwdOk" @change="mustUser()">
            记住密码
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button
            style="width: 100%"
            type="primary"
            size="large"
            @click="submitForm(ruleFormRef)"
          >
            立即登录
          </el-button>
        </el-form-item> </el-form
      ><br />
      <div style="display: flex; justify-content: right; align-items: center">
        <button
          style="color: #6c85f7; border: none; background-color: white"
          @click="$router.push('/forget_password')"
        >
          忘记密码?
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, watch, watchEffect } from "vue";
import { useRouter } from "vue-router";
import { useLoginStore } from "../Store/Login";
import { login } from "../axios/Api/xsp_api";
import { ElMessage } from "element-plus";
let store = useLoginStore();
const router = useRouter();
const formSize = ref("default");
const ruleFormRef = ref();
const ruleForm = reactive({
  username: JSON.parse(localStorage.getItem("username")) || "",
  password: JSON.parse(localStorage.getItem("password")) || "",
});

// 是否记住密码
const pwdOk = ref(false);
const mustUser = () => {
  console.log(pwdOk.value);
  if (pwdOk.value) {
    console.log(1);
    localStorage.setItem("username", JSON.stringify(ruleForm.username));
    localStorage.setItem("password", JSON.stringify(ruleForm.password));
  } else {
    localStorage.removeItem("username");
    localStorage.removeItem("password");
  }
};
watchEffect(() => {
  if (
    JSON.parse(localStorage.getItem("username")) &&
    JSON.parse(localStorage.getItem("password"))
  ) {
    pwdOk.value = true;
  } else {
    pwdOk.value = false;
  }
});
// 表单校验
const rules = {
  username: [{ required: true, message: "请输入账号", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};

const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const {
        data: { accessToken ,refreshToken},
      } = await login(ruleForm);
      if (accessToken) {
        store.Login({accessToken ,refreshToken})
        router.push("/home");
        ElMessage({
          message: "登录成功",
          type: "success",
        });
      } else {
        console.log("error submit!", fields);
      }
    }
  });
};
</script>


<style scoped>
* {
  padding: 0;
  margin: 0;
}
.login {
  width: 100%;
  height: 100%;
  position: relative;
}
.title {
  width: 95%;
  height: 20%;
  display: flex;
  justify-content: right;
  align-items: center;
}
h1 {
  color: #5676f7;
  font-style: italic;
  font-size: 50px;
}
.banner {
  width: 100%;
  height: 65%;
  background-image: url(../../public/1711072153790.jpg);
  background-size: 100% 100%;
  overflow: hidden;
  background-position: center;
  background-repeat: no-repeat;
}
.footer {
  box-sizing: border-box;
  box-shadow: 0 0 10px #5676f7;
  width: 20%;
  height: 50%;
  padding: 20px;
  position: absolute;
  top: 200px;
  left: 80px;
  background-color: #ffffff;
}
h2 {
  color: #5676f7;
}
#title {
  color: #5676f7;
}
</style>